package hips.images.floating.gray32;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import hips.images.floating.PixelValueFloat;

public class PixelValueGray32 implements PixelValueFloat<PixelValueGray32> {

	private static final NumberFormat formatter = new DecimalFormat("0.0#");
	private float[] slices;

	public PixelValueGray32(int n) {
		slices = new float[n];
	}

	public void setLower(PixelValueGray32 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] < slices[i]) {
				slices[i] = p.slices[i];
			}
		}
	}

	public void setGreater(PixelValueGray32 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] > slices[i]) {
				slices[i] = p.slices[i];
			}
		}
	}

	public boolean isEqual(PixelValueGray32 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] != slices[i]) {
				return false;
			}
		}
		return true;
	}

	public boolean isGreaterOrEqual(PixelValueGray32 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] > slices[i]) {
				return false;
			}
		}
		return true;
	}

	public boolean isLowerOrEqual(PixelValueGray32 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] < slices[i]) {
				return false;
			}
		}
		return true;
	}

	public PixelValueGray32 range(PixelValueGray32 p) {
		PixelValueGray32 pRange = new PixelValueGray32(slices.length);
		for (int i = 0; i < slices.length; i++) {
			pRange.slices[i] = Math.abs(slices[i] - p.slices[i]);
		}
		return pRange;
	}

	public PixelValueGray32 range(PixelValueGray32 pvalue1, PixelValueGray32 pvalue2) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] = Math.abs(pvalue1.slices[i] - pvalue2.slices[i]);
		}
		return this;
	}

	public float maxValueFloat() {
		float max = slices[0];
		for (int i = 1; i < slices.length; i++) {
			if (max < slices[i]) {
				max = slices[i];
			}
		}
		return max;
	}

	public float minValueFloat() {
		float min = slices[0];
		for (int i = 1; i < slices.length; i++) {
			if (min > slices[i]) {
				min = slices[i];
			}
		}
		return min;
	}

	public String getString() {
		if (slices.length == 1){
			return getString(0);
		}
		String returned = "(";
		for (int i = 0; i < slices.length - 1; i++) {
			returned += getString(i) + ",   ";
		}
		return returned + getString(slices.length - 1) + ")";
	}

	public String getString(int slice) {
		return formatter.format(slices[slice]).replace(",", ".");
	}

	public void setValueFloat(float value, int slice) {
		slices[slice] = value;
	}

	public float getValueFloat(int slice) {
		return slices[slice];
	}

	public int getSlices() {
		return slices.length;
	}

	public PixelValueGray32 add(PixelValueGray32 p) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] += p.slices[i];
		}
		return this;
	}

	public PixelValueGray32 add(float value) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] += value;
		}
		return this;
	}

	public PixelValueGray32 sub(PixelValueGray32 p) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] -= p.slices[i];
		}
		return this;
	}

	public PixelValueGray32 sub(float value) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] -= value;
		}
		return this;
	}

	public PixelValueGray32 mult(float value) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] *= value;
		}
		return this;
	}

	public PixelValueGray32 div(float value) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] /= value;
		}
		return this;
	}

	public PixelValueGray32 copy() {
		PixelValueGray32 p = new PixelValueGray32(slices.length);
		System.arraycopy(slices, 0, p.slices, 0, slices.length);
		return p;
	}

	public void setValueFloat(float value, int slice, int channel) {
		setValueFloat(value, slice);
	}

	public float getValueFloat(int slice, int channel) {
		return getValueFloat(slice);
	}

	public float sumFloat() {
		float sum = 0f;
		for (int i = 0; i < slices.length; i++) {
			sum += slices[i];
		}
		return sum;
	}

	public void setValue(Number value, int slice) {
		setValueFloat(value.floatValue(), slice);
	}

	public void setValue(Number value, int slice, int channel) {
		setValueFloat(value.floatValue(), slice, channel);
	}

	public Float getValue(int slice) {
		return getValueFloat(slice);
	}

	public Float getValue(int slice, int channel) {
		return getValueFloat(slice, channel);
	}

	public Float minValue() {
		return minValueFloat();
	}

	public Float maxValue() {
		return maxValueFloat();
	}

	public Float sum() {
		return sumFloat();
	}
}
